import sys
import pygame
import screen
import game_screen
import highscore_screen

#Note: sound from http://www.soundjay.com/button-sounds-1.html
class MenuScreen(screen.Screen):
    def __init__(self):
        screen.Screen.__init__(self)
        self.menu_screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT), pygame.DOUBLEBUF | pygame.HWSURFACE)
        self.menu_sound = pygame.mixer.Sound('../res/sounds/button-32.ogg')
        self.bg = pygame.image.load("../res/images/bluedragon.png").convert()
        self.bg_rect = self.bg.get_rect()
        #Typical initializing behaviors Sam used in other screens - Max
        self.width = self.menu_screen.get_width()
        self.height = self.menu_screen.get_height()
        self.font = pygame.font.Font("../res/fonts/finalf.ttf", 18) 
        self.time = 0.0
        self.states = []
        self.menu_states = ["- Start Game -", "- High Scores -", "- Options -", "- Exit -"]
        self.option_states = ["- Brightness -", "- Audio -", "- Return -"]
        
        self.option_state = 0
        self.current_state = 0
        self.option_current_state = 0
        self.states = self.menu_states
        
    def render(self):
         #self.menu_screen.fill((72,24,190))
        self.menu_screen.blit(self.bg, self.bg_rect)
        #We can come up with a more elaborate highlighting method later - Max
        #TODO Add ways to blit option_states
        for i in range(0, len(self.states)):
            if not self.option_current_state: 
                if i != self.current_state:
                    surf = self.font.render(self.states[i].upper(), True, (0, 0, 0))
                    self.menu_screen.blit(surf, (self.width / 2 - 30, self.height / 2 + i * 30))
                else:
                     surf = self.font.render(self.states[self.current_state].upper(), True, (255, 255, 0))
                     self.menu_screen.blit(surf, (self.width / 2 - 30, self.height / 2 + self.current_state * 30))
                

    def update(self, time):
        self.time += time
        
    def process_events(self, change_state):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()
                if self.option_state == 0:
                    self.process_menu(change_state, event)
                else:
                    self.process_options(change_state, event)
              
                        #TODO add sounds
    def process_menu(self, change_state, event):
          if event.key == pygame.K_RETURN:
                    if self.current_state == 0:
                        change_state(game_screen.GameScreen())
                    elif self.current_state == 1:
                        change_state(highscore_screen.HighScoreScreen())
                    elif self.current_state == 2:
                        self.states = self.option_states
                        self.option_state = 1
                    elif self.current_state == 3:
                        pygame.quit()
                        sys.exit()
                   
          elif event.key == pygame.K_DOWN:
                    self.menu_sound.play()
                    if self.current_state == 3:
                        self.current_state = 0
                    else:
                        self.current_state += 1
                        #TODO add sounds
          elif event.key == pygame.K_UP:
                    self.menu_sound.play()
                    if self.current_state == 0:
                        self.current_state = 3
                    else:
                        self.current_state -= 1
    
    def process_options(self, change_state, event):
        self.menu_sound.play()
        if event.key == pygame.K_RETURN:
                    if self.current_state == 0:
                        pass #Brightness
                    elif self.current_state == 1:
                       pass #Audio
                    elif self.current_state == 2:
                        self.states = self.menu_states
                        self.option_state = 0
                    elif self.current_state == 3:
                        self.option_state = 0
        elif event.key == pygame.K_DOWN:
                    self.menu_sound.play()
                    if self.current_state == 2:
                        self.current_state = 0
                    else:
                        self.current_state += 1
                        #TODO add sounds
        elif event.key == pygame.K_UP:
                    self.menu_sound.play()
                    if self.current_state == 0:
                        self.current_state = 2
                    else:
                        self.current_state -= 1
